home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
3_0
/
COLOR_OV
/
COLOROVA.C
< prev
Wrap
C/C++ Source or Header
|
1989-04-14
|
4KB
|
156 lines
/*
* ColorOvals
*
* An example of how to write a simple color program
* that will work on all Macs, with all ROMS, versions
* of system software, etc. This program uses classic
* Quickdraw's colors, so it doesn't check for any
* particular configuration.
*
* Brian Bechtel. Public domain.
*
*/
#include <QuickDraw.h>
#include <EventMgr.h>
#include <OSUtil.h>
#include <WindowMgr.h>
#define BOXOVERLAP 20
Str255 about = "\pColorOvals by Brian Bechtel";
/*
* Return a pseudo random integer, using the
* Mac OSUtility function Random()
*/
int
randomize(x)
int x;
{
int x2 = Random() % x;
if (x2 < 0) x2 = -x2;
return(x2);
}
main()
{
WindowPtr w;
Rect rct;
int i, j, l, t, r, b;
int bottom, right;
/*
* You have to initialize the GrafPort, and Windows,
* and you might as well do the cursor so you get an
* arrow instead of a watch.
*/
InitGraf(&thePort);
InitWindows();
InitCursor();
/*
* Set the seed so that we see different displays each time
* we run.
*/
GetDateTime(&randSeed);
/*
* I'm going to be using these two values a lot, so I
* store them in local storage.
*/
bottom = screenBits.bounds.bottom;
right = screenBits.bounds.right;
/*
* Build a window the size of the screen.
*/
SetRect(&rct, 0, 0, right, bottom);
w = NewWindow(0L, &rct, "\pno title", true, plainDBox,
(WindowPtr *)-1, true, 0L);
SetPort(w);
/*
* Calling InitPort for this port (remember, a WindowPtr
* contains within it a GrafPort as the first elements)
* means that I will cover the menubar, as well as the
* rest of the screen. Set background pattern to the
* default black and erase the screen.
*/
InitPort(w);
BackPat(w->pnPat);
EraseRect(&(w->portRect));
/*
* Loop until the user hits the mouse button, building
* a random sized rectangle. About half the time, we
* set a new foreground color, and paint a new oval.
* About half the time, we invert the oval contained in
* this random rectangle.
*/
do {
l = randomize(right);
t = randomize(bottom);
r = randomize(right);
b = randomize(bottom);
SetRect(&rct, l, t, r, b);
i = randomize(20);
if (i < 10) {
switch (i) {
case 1: ForeColor(whiteColor); break;
case 2: ForeColor(redColor); break;
case 3: ForeColor(greenColor); break;
case 4: ForeColor(blueColor); break;
case 5: ForeColor(cyanColor); break;
case 6: ForeColor(magentaColor); break;
case 7: ForeColor(yellowColor); break;
default: ForeColor(blackColor); break;
}
j = randomize(4);
switch (j) {
case 0: FillOval(&rct, black); break;
case 1: FillOval(&rct, gray); break;
case 2: FillOval(&rct, ltGray); break;
case 3: FillOval(&rct, dkGray); break;
}
}
else
InvertOval(&rct);
SystemTask(); /* let background stuff happen,
since we may be in this loop
a while.
*/
} while (!Button());
/*
* Okay, the user hit the button. Wait for them to let up
* on the button (otherwise, we'd read the button too fast
* and exit before we show them who we are.
*/
while (Button()) {};
FlushEvents(everyEvent, 0);
/*
* Build a black rectangle and write a white string into it
* to show the user who we are. I could try to find the font
* height (using GetFontInfo()), but I'm just approximating
* everything by a constant of BOXOVERLAP.
*/
ForeColor(blackColor);
l = (right-StringWidth(about))/2;
t = bottom/2;
MoveTo(l, t);
r = l+StringWidth(about)+BOXOVERLAP;
b = t+BOXOVERLAP;
SetRect(&rct, l-BOXOVERLAP, t-BOXOVERLAP, r, b);
EraseRect(&rct);
ForeColor(whiteColor);
DrawString(about);
while (!Button()) {};
FlushEvents(everyEvent, 0);
DisposeWindow(w);
}